package in.srain.cube.util;

import android.app.NotificationChannel;
import android.app.NotificationChannelGroup;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.os.Looper;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationManagerCompat;

import java.util.List;

public final class CheckUtil {

    private CheckUtil() {
    }

    public static <T> T checkNotNull(T value) {
        if (value == null) {
            throw new NullPointerException();
        }
        return value;
    }

    public static <T> T checkNotNull(T value, String message) {
        if (value == null) {
            throw new NullPointerException(message);
        }
        return value;
    }

    public static void checkNotMain() {
        if (isMain()) {
            throw new IllegalStateException("Method call should not happen from the main thread.");
        }
    }

    public static void checkMain() {
        if (!isMain()) {
            throw new IllegalStateException("Method call should happen from the main thread.");
        }
    }

    public static boolean isMain() {
        return Looper.getMainLooper().getThread() == Thread.currentThread();
    }

    public static boolean checkAppsIsExist(Context context, Intent intent) {
        PackageManager packageManager = context.getPackageManager();
        List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        return activities.size() > 0;
    }

    public static boolean checkAppIsExist(Context context, String packageName) {
        PackageManager packageManager = context.getPackageManager();
        try {
            return packageManager.getPackageInfo(packageName, PackageManager.GET_META_DATA) != null;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean isNotificationChannelEnabled(Context context, @NonNull String groupId, @NonNull String... channelIds) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            boolean appNotificationEnable = NotificationManagerCompat.from(context).areNotificationsEnabled();
            if (!appNotificationEnable) {
                return false;
            }

            NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                List<NotificationChannelGroup> groupList = manager.getNotificationChannelGroups();
                for (NotificationChannelGroup group : groupList) {
                    if (TextUtils.equals(group.getId(), groupId)) {
                        if (group.isBlocked()) {
                            return false;
                        }
                    }
                }
            }

            for (String channelId : channelIds) {
                NotificationChannel channel = manager.getNotificationChannel(channelId);
                if (channel != null && channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
                    return false;
                }
            }
            return true;
        }

        return true;
    }
}
